package com.bruce.ai.alibaba.agentcard.discovery.circuitbreaker;

import java.time.Duration;

/**
 * 熔断器指标
 */
public class CircuitBreakerMetrics {
    
    private final long totalRequests;
    private final long totalSuccesses;
    private final long totalFailures;
    private final int currentFailureCount;
    private final int currentSuccessCount;
    private final CircuitBreaker.State currentState;
    private final Duration timeInCurrentState;
    
    public CircuitBreakerMetrics(long totalRequests, 
                                long totalSuccesses, 
                                long totalFailures,
                                int currentFailureCount,
                                int currentSuccessCount,
                                CircuitBreaker.State currentState,
                                Duration timeInCurrentState) {
        this.totalRequests = totalRequests;
        this.totalSuccesses = totalSuccesses;
        this.totalFailures = totalFailures;
        this.currentFailureCount = currentFailureCount;
        this.currentSuccessCount = currentSuccessCount;
        this.currentState = currentState;
        this.timeInCurrentState = timeInCurrentState;
    }
    
    /**
     * 获取总请求数
     */
    public long getTotalRequests() {
        return totalRequests;
    }
    
    /**
     * 获取总成功数
     */
    public long getTotalSuccesses() {
        return totalSuccesses;
    }
    
    /**
     * 获取总失败数
     */
    public long getTotalFailures() {
        return totalFailures;
    }
    
    /**
     * 获取当前失败计数
     */
    public int getCurrentFailureCount() {
        return currentFailureCount;
    }
    
    /**
     * 获取当前成功计数
     */
    public int getCurrentSuccessCount() {
        return currentSuccessCount;
    }
    
    /**
     * 获取当前状态
     */
    public CircuitBreaker.State getCurrentState() {
        return currentState;
    }
    
    /**
     * 获取在当前状态的时间
     */
    public Duration getTimeInCurrentState() {
        return timeInCurrentState;
    }
    
    /**
     * 计算成功率
     */
    public double getSuccessRate() {
        if (totalRequests == 0) {
            return 0.0;
        }
        return (double) totalSuccesses / totalRequests;
    }
    
    /**
     * 计算失败率
     */
    public double getFailureRate() {
        if (totalRequests == 0) {
            return 0.0;
        }
        return (double) totalFailures / totalRequests;
    }
    
    /**
     * 检查熔断器是否健康
     */
    public boolean isHealthy() {
        return currentState == CircuitBreaker.State.CLOSED && getFailureRate() < 0.1;
    }
    
    /**
     * 检查熔断器是否不可用
     */
    public boolean isUnavailable() {
        return currentState == CircuitBreaker.State.OPEN;
    }
    
    /**
     * 检查熔断器是否在恢复中
     */
    public boolean isRecovering() {
        return currentState == CircuitBreaker.State.HALF_OPEN;
    }
    
    @Override
    public String toString() {
        return "CircuitBreakerMetrics{" +
                "totalRequests=" + totalRequests +
                ", totalSuccesses=" + totalSuccesses +
                ", totalFailures=" + totalFailures +
                ", currentFailureCount=" + currentFailureCount +
                ", currentSuccessCount=" + currentSuccessCount +
                ", currentState=" + currentState +
                ", timeInCurrentState=" + timeInCurrentState +
                ", successRate=" + String.format("%.2f%%", getSuccessRate() * 100) +
                ", failureRate=" + String.format("%.2f%%", getFailureRate() * 100) +
                '}';
    }
    
    /**
     * 转换为JSON格式的字符串
     */
    public String toJson() {
        return "{" +
                "\"totalRequests\":" + totalRequests + "," +
                "\"totalSuccesses\":" + totalSuccesses + "," +
                "\"totalFailures\":" + totalFailures + "," +
                "\"currentFailureCount\":" + currentFailureCount + "," +
                "\"currentSuccessCount\":" + currentSuccessCount + "," +
                "\"currentState\":\"" + currentState + "\"," +
                "\"timeInCurrentStateMs\":" + timeInCurrentState.toMillis() + "," +
                "\"successRate\":" + getSuccessRate() + "," +
                "\"failureRate\":" + getFailureRate() + "," +
                "\"isHealthy\":" + isHealthy() + "," +
                "\"isUnavailable\":" + isUnavailable() + "," +
                "\"isRecovering\":" + isRecovering() +
                "}";
    }
}